{% extends 'base.html' %}

{% block title %}用户管理{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>用户管理</h1>
    <a href="{{ url_for('user.add_user') }}" class="btn btn-primary">添加新用户</a>
</div>

<!-- 添加搜索表单 -->
<div class="card mb-4">
    <div class="card-body">
        <form method="get" action="{{ url_for('user.user_management') }}" class="row g-3">
            <div class="col-md-10">
                <input type="text" class="form-control" name="search" placeholder="按用户名、姓名或邮箱搜索..." value="{{ request.args.get('search', '') }}">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-secondary w-100">搜索</button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>姓名</th>
                        <th>邮箱</th>
                        <th>状态</th>
                        <th>角色</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.full_name }}</td>
                        <td>{{ user.email }}</td>
                        <td>
                            {% if user.status == 'active' %}
                                <span class="badge bg-success">在用</span>
                            {% else %}
                                <span class="badge bg-secondary">停用</span>
                            {% endif %}
                        </td>
                        <td>
                            {% if user.role == 'admin' %}
                                <span class="badge bg-primary">管理员</span>
                            {% elif user.role == 'business_user' %}
                                <span class="badge bg-info">业务用户</span>
                            {% else %}
                                <span class="badge bg-warning">部门经理</span>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ url_for('user.edit_user', id=user.id) }}" class="btn btn-sm btn-outline-primary">编辑</a>
                            <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ user.id }}">删除</button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

{# 将所有模态框移到表格外部 #}
{% for user in users %}
<div class="modal fade" id="deleteModal{{ user.id }}" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除用户 <strong>{{ user.username }}</strong> 吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form action="{{ url_for('user.delete_user', id=user.id) }}" method="post">
                    <button type="submit" class="btn btn-danger">删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endfor %}
{% endblock %}